import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, PhysicsSystem2D, RigidBody, RigidBody2D, v2, director, game, Node, Prefab, instantiate, resources, loader, assetManager, v3, Vec3 } from 'cc';
import { game_node } from '../canvas/gameNode/game_node';
import { camera } from '../canvas/camera/camera';
const { ccclass, property } = _decorator;

@ccclass('player_crash')
export class player_crash extends Component {
    //判断是否可执行对话
    public static allow_dialog = false
    //存放预设体
    private pre = null

    onLoad() {
    }
    start() {
        // 注册单个碰撞体的回调函数，和双方tag有关，谁是碰撞者，谁是被碰撞着——暂时先用这个
        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
            collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
            collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
        }


        // 注册全局碰撞回调函数
        // if (PhysicsSystem2D.instance) {
        //     PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        //     PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        //     PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
        //     PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
        // }

    }


    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null): void {
        // 只在两个碰撞体开始接触时被调用一次
        console.log("碰撞执行正常")
        //self代表当前tag,other代表其他tag,在component里面设置
        console.log(selfCollider.tag, otherCollider.tag)
        //player_1碰撞到bulid_100,加载场景2预设体
        if (selfCollider.tag == 1 && otherCollider.tag == 100) {
            //关闭相机跟踪player
            camera.camera_follow_player = false
            //如果是tank状态
            if (!camera.state_human) {
                //停止坦克移动
                camera.tank_player_move = false
                //重置player坐标
                // camera.tank_player.setPosition(0,0) //这句话正常，但是坦克的位置已经被抢先赋值了,vec3异常记录提示：“这个值在你赋值前，可能已经被赋值了”
                setTimeout(() => {
                    //设置坦克位置
                    camera.tank_player.setPosition(0, 0)
                    //重置炮塔位置
                    camera.tank_player_Gun.position = camera.tank_player.position
                    //开启坦克移动事件
                    camera.tank_player_move = true
                }, 1000);
            } else if(camera.state_human){
                //重置human坐标
                setTimeout(() => {
                    //设置human位置
                    camera.player.setPosition(0, 0)
                }, 1000);
            }


            //摧毁节点下的子节点和事件，此方法不会移除子节点内容
            try {
                game_node.game_node.destroyAllChildren()
            } catch {
                console.log("捕获摧毁节点异常")
            }

            // 清空节点gameNode
            // 清除成功，but uncaught，并且节点依然可以被碰撞
            // try { game_node.game_node.removeAllChildren()
            // }catch {
            //     console.log("捕获removeAll异常")
            // }

            //预设体属性赋值、预设体加载
            resources.load("prefab/prefab_text", Prefab, (err, prefab) => {
                //实例化预设体
                this.pre = instantiate(prefab)
                //game_node节点添加预设体
                game_node.game_node.addChild(this.pre)
            })
        }

        if (selfCollider.tag == 1 && otherCollider.tag == 1) {
            //允许人类状态player上车
            console.log("允许人类上车")
            camera.human_getOn = true;
        }
    }

    onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体结束接触时被调用一次
        // 不允许对话
        if (selfCollider.tag == 1 && otherCollider.tag == 2) {
            console.log("与'三杉'接触取消，对话按钮不可用")
            player_crash.allow_dialog = false
        }

        if (selfCollider.tag == 1 && otherCollider.tag == 1) {
            //人类上车关闭
            console.log("人类上车功能关闭")
            camera.human_getOn = false;
        }
    }

    //这里做对话逻辑_需要自己完善player面向_正对方向
    onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 每次将要处理碰撞体接触逻辑时被调用
        // console.log('准备碰撞');
        if (selfCollider.tag == 1 && otherCollider.tag == 2) {
            //允许对话..执行
            player_crash.allow_dialog = true
        }

    }
    onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 每次处理完碰撞体接触逻辑时被调用

    }


    update(deltaTime: number) {

    }
}

